package com.cc.service.performance;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.List;
import java.util.stream.Collectors;

import com.cc.infrastructure.inter.performance.PerformanceInter;
import com.cc.web.vo.PerformanceVo;
import com.cc.infrastructure.db.PerformanceDb;
import com.cc.pojo.dto.PerformanceDto;
import com.cc.infrastructure.special.dto.PerformanceDo;

/**
 * @author C.c
 **/
@Service
public class PerformanceService {
  @Resource
  private PerformanceInter performanceInter;

  /**
   * 分页
   *
   * @author C.c
   **/
  public Page<PerformanceVo> query(String name, int pageNum, int pageSize) {
    Page<PerformanceDo> performanceDoDos = performanceInter.query(name, pageNum, pageSize);
    List<PerformanceVo> collect = performanceDoDos.stream().map(PerformanceVo::convertPerformanceVoByPerformanceDo).collect(Collectors.toList());
    return new PageImpl<>(collect, performanceDoDos.getPageable(), performanceDoDos.getTotalElements());
  }

  /**
   * 新增
   *
   * @author C.c
   **/
  @Transactional(rollbackFor = Exception.class)
  public boolean add(PerformanceDto performanceDto) {
    PerformanceDb performancedb = PerformanceDto.convertPerformanceDbByPerformanceDto(performanceDto);
    performancedb.setId(UUID.randomUUID().toString());
    performanceInter.save(performancedb);
    return true;
  }

  /**
   * 删除
   *
   * @param id
   * @return boolean
   * @author C.c
   **/
  public boolean delete(String id) {
    performanceInter.deleteById(id);
    return true;
  }

  /**
   * 修改
   *
   * @return boolean
   * @author C.c
   **/
  @Transactional(rollbackFor = Exception.class)
  public boolean edit(PerformanceDto performanceDto) {
    PerformanceDb performancedb = PerformanceDto.convertPerformanceDbByPerformanceDto(performanceDto);
    performanceInter.save(performancedb);
    return true;
  }

  /**
   * 查询单个
   *
   * @param id 唯一标识
   * @author C.c
   **/
  public PerformanceVo view(String id) {
    PerformanceDb performancedb = performanceInter.findById(id).orElse(null);
    return PerformanceVo.convertPerformanceVoByPerformanceDo(performancedb);
  }
}